class Batcher {
  constructor() {
    this.reset()
  }
  reset() {
    this.queue = []
    this.has = {}
    this.waiting = false
  }
  flush () {
    this.queue.forEach(job => {
      job.cb()
    })
    this.reset()
  }
  push(job) {
    let id = job.id
    if (this.has[id]) return
    this.queue.push(job)
    this.has[id] = true
    if (this.waiting) return
    this.waiting = true
    Promise.resolve().then(() => {
      this.flush()
    })
  }
}